/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Application;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import Maps.*;
import javax.microedition.lcdui.Image;



/**
 *
 * @author Stanley
 */

interface ZoomLevel {
    public static final int DEFAULT = 14;
    public static final int MAX = 21;
    public static final int MIN = 0;
}

interface MapType{
    public static final String DEFAULT = "roadmap";
    public static final String SATELLITE = "satellite";
    public static final String TERRAIN = "terrain";
    public static final String HYBRID = "hybrid";
}

public class Home extends Canvas implements CommandListener {
    private MainMidlet midlet;
    
    // Maps
    private  GoogleMaps maps;
    private Image map; // static image map that's retrieved from maps.google.com
    private int zoomLevel = ZoomLevel.DEFAULT;
    private double[] lanLng; // coordinates
    private String maptype = MapType.DEFAULT;
    private int[] mapCenter;
    
    
    // command
    private  Command exit;
    private  Command options;
    
    public Home (MainMidlet midlet) throws Exception {
        this.midlet = midlet;
        // center point of maps (image)
        mapCenter = new int[2];
        mapCenter[0] = 0;
        mapCenter[1] = 0;
        
        // maps
        maps = new GoogleMaps();
        // get coordinates
        lanLng = maps.geocodeAddress("Ho chi minh");
        // get map image
        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoomLevel, "png32", MapType.DEFAULT);
        
        // add command
        exit = new Command("Exit", Command.EXIT, 1);
        options = new Command("Options", Command.ITEM, 1);
        setCommandListener(this);
        addCommand(exit);
        addCommand(options);        
    }
    
    public  void paint (Graphics g) {
        try {
            // set default value to mapCenter
            mapCenter[0] = mapCenter[1] = 0;
            g.drawImage(getMap(), 0, 0, Graphics.TOP | Graphics.LEFT);            
        } catch (Exception e) {
        }
    }
    
    public  void commandAction (Command c, Displayable d) {
        if (c == exit) {
            midlet.exitMIDlet();
        } else if (c == options) {
            midlet.optionsShow();
        }
    }
    
    protected void keyPressed(int keyCode) {
        int gameAction = getGameAction(keyCode);
        try {
            switch(gameAction) {
                case GAME_B:
                    // zoom +
                    zoomOut();               
                    break;
                case GAME_A:
                    // zoom -
                    zoomIn();
                    break;
                case UP: 
                    scrollingUp();
                    break;
                case DOWN: // scrolling down
                    scrollingDown();
                    break;
                case LEFT: // scrolling left
                    scrollingLeft();
                    break;
                case RIGHT:// scrolling right
                    scrollingRight();
                    break;
                case FIRE: // find coordinate
                    midlet.findCoordinatesShow();
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }        
        
        // repaint the maps
        repaint();
    }
    
    /**
     * UTILITY METHOD FOR KEY PRESSED
     */
    private void zoomOut() throws IOException{
        if(zoomLevel < ZoomLevel.MAX) {
            setZoomLevel(zoomLevel + 1); // increase zoom level
            // get map image
            setMap(maps.retrieveStaticImage(this.getWidth(), this.getHeight(), getLanLng()[0], getLanLng()[1], zoomLevel, "png32", maptype));
        }
    }
    
    private void zoomIn () throws IOException {
        if(zoomLevel > ZoomLevel.MIN) {
            setZoomLevel(zoomLevel - 1); // decrease zoom level
            // get map image
            setMap(maps.retrieveStaticImage(this.getWidth(), this.getHeight(), getLanLng()[0], getLanLng()[1], zoomLevel, "png32", maptype));
        }
    }

    /**
     * @return the map
     */
    public Image getMap() {
        return map;
    }

    /**
     * @param map the map to set
     */
    public void setMap(Image map) {
        this.map = map;
    }

    /**
     * @return the lanLng
     */
    public double[] getLanLng() {
        return lanLng;
    }

    /**
     * @param lanLng the lanLng to set
     */
    public void setLanLng(double[] lanLng) {
        this.lanLng = lanLng;
    }

    /**
     * @param zoomLevel the zoomLevel to set
     */
    public void setZoomLevel(int zoomLevel) {
        this.zoomLevel = zoomLevel;
    }
    
    public int getZoomLevel(){
        return this.zoomLevel;
    }

    /**
     * @param maptype the map type to set
     */
    public void setMaptype(String maptype) {
        this.maptype = maptype;
    }
    
    public String getMapType() {
        return this.maptype;
    }
    
    public void scrollingUp() throws IOException{
        mapCenter[1] = -40;
        GoogleMaps maps = new GoogleMaps();
        lanLng = maps.adjust(lanLng[0], lanLng[1], mapCenter[0], mapCenter[1], zoomLevel);
        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoomLevel, "png32", maptype);
    }
    
    public void scrollingDown() throws IOException{
        mapCenter[1] = 40;
        GoogleMaps maps = new GoogleMaps();
        lanLng = maps.adjust(lanLng[0], lanLng[1], mapCenter[0], mapCenter[1], zoomLevel);
        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoomLevel, "png32", maptype);
    }
    
    public void scrollingLeft() throws IOException{
        mapCenter[0] = -40;
        GoogleMaps maps = new GoogleMaps();
        lanLng = maps.adjust(lanLng[0], lanLng[1], mapCenter[0], mapCenter[1], zoomLevel);
        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoomLevel, "png32", maptype);
    }
    
    public void scrollingRight() throws IOException{
        mapCenter[0] = 40;
        GoogleMaps maps = new GoogleMaps();
        lanLng = maps.adjust(lanLng[0], lanLng[1], mapCenter[0], mapCenter[1], zoomLevel);
        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoomLevel, "png32", maptype);
    }
}
